using System.Collections.Generic;

namespace ColladaDotNet
{
	public partial class material : ColladaXmlNode
	{
		public override string GetId() { return id; }
		public override IList<ColladaXmlNode> ChildNodes
		{
			get
			{
				var list = new List<ColladaXmlNode>();
				if (extra != null)
					foreach (object e in extra) if (e is ColladaXmlNode) list.Add((ColladaXmlNode)e);
				if (asset != null)
					list.Add(asset);
				if (instance_effect != null)
					list.Add(instance_effect);
				return list;
			}
		}

		public image GetDiffuseImage(COLLADA doc)
		{
			var e = doc.GetElementByUrl(this.instance_effect.url) as effect;
			if (e == null)
				return null;
			var d = e.GetDiffuse(doc);
			if (d.Item is common_color_or_texture_typeTexture)
			{
				var texSid = ((common_color_or_texture_typeTexture)d.Item).texture;
				var sampler = e.GetElementBySid(texSid) as common_newparam_type;
				var fx_sampler2D_common = sampler.Item as fx_sampler2D_common;
				var surface = e.GetElementBySid(fx_sampler2D_common.source) as common_newparam_type;
				var fx_surface_common = surface.Item as fx_surface_common;
				return doc.GetElementById(fx_surface_common.init_from[0].Value) as image;
			}
			return null;
		}
	}
}
